﻿/*
 *  TextDownload.cs - Class the provides text download functionality
 *
 *  Copyright (C) 2008 dreamlusion <dreamlusion@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License version
 *      2 as published by the Free Software Foundation.
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace phpMyAdminToolkit.Common.Net
{
	public class TextDownload : Download
	{
		public string Do()
		{
			HttpWebRequest request;
			HttpWebResponse response = null;

			try
			{
				request = (Request == null) 
					? WebRequest.Create(SourceUri) as HttpWebRequest 
					: Request;
				response = request.GetResponse() as HttpWebResponse;
				Stream s = response.GetResponseStream();

				string text = string.Empty;
				using (StreamReader sr = new StreamReader(s))
				{
					text = sr.ReadToEnd();
				}

				// Close everything.
				s.Close();
				response.Close();

				return text;

			}
			catch (System.Net.WebException)
			{
				if (response != null)
					response.Close();

				throw;
			}
		}
	}
}
